package com.xhu.water_supply.service.impl;

import com.xhu.water_supply.entity.Administrator;
import com.xhu.water_supply.mapper.AdministratorMapper;
import com.xhu.water_supply.query.AdminQuery;
import com.xhu.water_supply.service.AdministratorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

//管理员业务的 实现类
@Service
@Transactional
public class AdministratorServiceImpl implements AdministratorService {
    @Autowired
    private AdministratorMapper administratorMapper;

    @Override
    public Administrator getByLoginAccount(String account) {
        //查询条件对象
        Example example = new Example(Administrator.class);
        //设置搜索的列
        example.selectProperties("id", "account", "password", "name", "picture");
        //设置搜索条件
        example.and().andEqualTo("account", account);
        return administratorMapper.selectOneByExample(example);
    }

    public boolean addAdministrator(Administrator administrator){
        return administratorMapper.insert(administrator) == 1;
    }

    @Override
    public List<Administrator> getByQuery(AdminQuery adminQuery) {
        Example example = new Example(Administrator.class);
        example.selectProperties("id", "account", "picture", "name");
        example.createCriteria()
                .andLike("account", "%" + adminQuery.getAccount() + "%")
                .orLike("name", "%" + adminQuery.getName() + "%");
        return administratorMapper.selectByExample(example);
    }
}
